package top.auntie.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.auntie.bean.IdDto;
import top.auntie.bean.PageReq;
import top.auntie.bean.Result;
import top.auntie.model.Type;
import top.auntie.service.TypeService;

@Slf4j
@RestController
@RequestMapping("/type")
public class TypeController {

    @Autowired
    private TypeService typeService;

    @GetMapping("/query")
    public Result queryType(PageReq pageReq) {
        return Result.success(typeService.queryType(pageReq));
    }

    @PostMapping("/save")
    public Result saveType(@RequestBody Type type) {
        typeService.saveType(type);
        return Result.success();
    }

    @GetMapping("/info")
    public Result getType(Integer id) {
        return Result.success(typeService.getTypeById(id));
    }

    @PostMapping("/del")
    public Result deleteType(@RequestBody IdDto idDto) {
        try {
            typeService.deleteType(idDto.getId());
            return Result.success();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.failed(e.getMessage());
        }
    }

    @GetMapping("/select")
    public Result typeSelect() {
        return Result.success(typeService.getAllTypes());
    }

}
